package com.chao.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqCallbackConfig implements ApplicationContextAware {

    // @Autowired
    // private RabbitTemplate rabbitTemplate;
    //
    // @Bean
    // public void CallbackConfig() {
    //     rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
    //         @Override
    //         public void returnedMessage(Message message, int i, String s, String s1, String s2) {
    //             System.out.println("message = " + message);
    //             System.out.println("i = " + i);
    //             System.out.println("s = " + s);
    //             System.out.println("s1 = " + s1);
    //             System.out.println("s2 = " + s2);
    //         }
    //     });
    // }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("message = " + message);
                System.out.println("i = " + i);
                System.out.println("s = " + s);
                System.out.println("s1 = " + s1);
                System.out.println("s2 = " + s2);
            }
        });
    }
}
